package com.pig.goods.service.impl;

import com.pig.exception.UnExceptDataException;
import com.pig.pojo.commgoods.Comment;
import com.pig.goods.dao.CommentDao;
import com.pig.exception.NoDataFoundException;
import com.pig.goods.service.CommentService;
import com.pig.pojo.commgoods.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;
    @Override
    public PageObject<Comment> findCommentByGoodsId(Integer goodsId, Integer pageCurrent) {
        if (StringUtils.isEmpty(goodsId) || goodsId == 0) {
            throw new IllegalArgumentException("id错误！");
        }
        if (pageCurrent == null || pageCurrent <= 0) {
            throw new IllegalArgumentException("页码不正确");
        }
        int pageSize = 5;
        List<Comment> comm = commentDao.findCommentByGoodsId(goodsId, (pageCurrent - 1) * pageSize, pageSize);
        if (comm == null || comm.size() == 0) {
            throw new NoDataFoundException("未曾查询到评论！");
        }
        PageObject<Comment> poj = new PageObject<>(pageSize,comm.size(),pageCurrent,comm);
        return poj;
    }

    @Override
    public int insertComment(Comment comment) {
        if (StringUtils.isEmpty(comment.getComm())) {
            throw new UnExceptDataException("请填写评论！");
        }
        if (StringUtils.isEmpty(comment.getUsername())) {
            throw new UnExceptDataException("用户不可为空！");
        }
        if (StringUtils.isEmpty(comment.getGoodsId())) {
            throw new UnExceptDataException("商品不可为空!");
        }
        comment.setCreatedTime(new Date());
        commentDao.insert(comment);
        return 0;
    }
}
